package com.muhuahealth.controller;

import com.muhuahealth.entity.RoadRepairEntity;
import com.muhuahealth.service.RoadRepairService;
import com.muhuahealth.utils.PageQueryUtil;
import com.muhuahealth.utils.PageResult;
import com.muhuahealth.utils.ServerResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author GSK
 * @version 1.0
 * @date 2021/1/24 18:14
 */
@SuppressWarnings("ALL")
@Log4j2
@Controller
@RequestMapping("/admin/roadRepair")
public class RoadRepairController {

    @Autowired
    RoadRepairService roadRepairService;

    /**
     * 跳转到道路大中修页面
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String roadRepairPage(HttpServletRequest request){
        log.info("跳转到道路大中修页面");
        request.getSession().setAttribute("path","Daily_items1");
        return "Daily_items1";
    }

    /**
     * 道路大中修展示列表
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse roadRepairList(@RequestParam Map<String,Object> params){

        if(StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))){
            return ServerResponse.createError("参数异常！");
        }

        PageQueryUtil queryUtil = new PageQueryUtil(params);
        PageResult totalPage = roadRepairService.getRoadRepairListPage(queryUtil);
        return ServerResponse.createSuccess("success",totalPage);
    }

    /**
     * 查询结果
     * @param repairEntity
     * @param model
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/searchResult", method = RequestMethod.POST)
    @ResponseBody
    public String searchResult(@RequestBody RoadRepairEntity repairEntity,
                               Model model, HttpSession httpSession){

        List<RoadRepairEntity> list = roadRepairService.getSearchResult(repairEntity);
        if(list.size() != 0){
//            httpSession.setAttribute("roadName",repairEntity.getRoadName());
//            httpSession.setAttribute("roadPlace", repairEntity.getRoadPlace());
            model.addAttribute("list",list);
            log.info("roadName:"+repairEntity.getRoadName());
            log.info("roadPlace:"+repairEntity.getRoadPlace());
            log.info("roadRepairEntity:" +repairEntity);
            log.info("list:" +list);
            return "Daily_items1::road_repair";
        }
        log.error(list);
        return null;
    }
}
